<template>
  <div class="app-container">
    <!-- 主内容区 -->
    <div class="main-content">
      <!-- 面包屑导航 -->
      <div class="breadcrumb">
        <span>我的项目</span>
        <div class="header-actions">
          <el-button icon="el-icon-back" @click="back">返回</el-button>
        </div>
      </div>

      <!-- 搜索框 -->
      <div class="search-box">
        <el-input v-model="searchKeyword" placeholder="搜索名称" clearable @input="handleSearch">
          <i slot="prefix" class="el-input__icon el-icon-search" />
        </el-input>
      </div>

      <!-- 加载状态 -->
      <div v-if="loading" class="loading-container">
        <el-progress type="circle" :percentage="50" :show-text="false" />
        <span style="margin-left: 10px;">加载中...</span>
      </div>

      <!-- 研究项目卡片网格 -->
      <div v-else class="project-grid">
        <!-- 项目卡片 -->
        <div
          v-for="project in projects"
          :key="project.id"
          class="project-card"
          :class="project.color"
          @click="handleCardClick(project)"
        >
          <div class="card-header">
            <h3>{{ project.title }}</h3>
            <div class="card-actions">
              <!-- <span class="icon"
                    @click="handleTeam(project)">👥</span>
              <span class="icon"
                    @click="handleSettings(project)">⚙️</span>
              <span class="icon"
                    @click="handleStatistics(project)">📊</span> -->
            </div>
          </div>
          <div class="card-body">
            <div class="researcher">
              研究负责人 : {{ project.chargePerson }}
            </div>
            <div class="progress-section">
              <!-- <div class="progress-item">
                <div class="progress-title">本周进度</div>
                <div class="timeline">
                  <div class="timeline-markers">
                    <span v-for="day in project.weekProgress"
                          :key="day"
                          class="marker">
                      {{ day }}
                    </span>
                  </div>
                  <div class="timeline-line"></div>
                </div>
              </div> -->
              <div class="progress-item">
                <div class="progress-title">项目进度</div>
                <div class="progress-circle">
                  <div class="circle-text">{{ project.progress }}%</div>
                </div>
                <!-- <div class="join-count">入组 {{ project.participants }}人</div> -->
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { projectCenter } from '@/api/menu'
export default {
  name: 'Index',
  data() {
    return {
      loading: true,
      searchKeyword: '',
      projects: [],
      researchId: null
    }
  },
  computed: {
  },
  mounted() {
    this.researchId = localStorage.getItem('CURRENT_RESEARCH_ID')
    this.init()
  },
  methods: {
    async init() {
      this.loading = true
      await projectCenter({ researchId: this.researchId }).then(res => {
        this.projects = res.data
        this.projects.forEach(project => {
          project.color = project.color || (project.id % 2 === 0 ? 'gray' : 'blue')
          project.progress = (project.current / project.target * 100).toFixed(1)
        })
        this.loading = false
      }).catch(err => {
        console.log(err)
        this.loading = false
      })
    },
    // 卡片点击事件
    handleCardClick(project) {
      localStorage.setItem('CURRENT_PROJECT_ID', project.id)
      // 跳转到项目详情页，携带项目ID
      this.$router.push({
        path: '/home'
      })
    },

    // 搜索处理
    handleSearch() {
      // 搜索逻辑已在computed属性中实现
    },

    // 团队管理
    handleTeam(project) {
      this.$message.success(`打开 ${project.name} 的团队管理`)
      console.log('团队管理:', project)
    },

    // 项目设置
    handleSettings(project) {
      this.$message.info(`打开 ${project.name} 的项目设置`)
      console.log('项目设置:', project)
    },

    // 数据统计
    handleStatistics(project) {
      this.$message.warning(`查看 ${project.name} 的数据统计`)
      console.log('数据统计:', project)
    },
    back() {
      this.$router.go(-1)
    }
  }
}
</script>

<style scoped>
body {
  background-color: #f5f7fa;
}

.app-container {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

/* 主内容区域样式 */
.main-content {
  flex: 1;
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
  width: 100%;
}

/* 面包屑导航 */
.breadcrumb {
  margin-bottom: 15px;
  font-size: 18px;
  font-weight: bold;
  color: #333;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* 搜索框 */
.search-box {
  margin-bottom: 20px;
}

.search-box input {
  width: 100%;
  padding: 10px 15px;
  border: 1px solid #ddd;
  border-radius: 6px;
  font-size: 14px;
  background-color: white;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
}

/* 项目网格 */
.project-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 20px;
}

/* 项目卡片 */
.project-card {
  background-color: white;
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  height: 100%;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.project-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.12);
}

.blue {
  background: linear-gradient(135deg, #aedfff 0%, #49b6ff 100%);
}

.gray {
  background: linear-gradient(135deg, #c5c3c3 0%, #818181 100%);
}

.card-header {
  padding: 15px 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: white;
  border-bottom: 1px solid rgba(255, 255, 255, 0.2);
}

.card-header h3 {
  font-size: 16px;
  font-weight: 600;
  margin: 0;
}

.card-actions {
  display: flex;
}

.header-actions {
  display: flex;
  gap: 10px;
}

.icon {
  margin-left: 15px;
  font-size: 18px;
  cursor: pointer;
  transition: transform 0.2s;
}

.icon:hover {
  transform: scale(1.2);
}

.card-body {
  padding: 20px;
}

.researcher {
  color: white;
  font-size: 14px;
  margin-bottom: 20px;
  opacity: 0.9;
}

.progress-section {
  display: flex;
  justify-content: space-between;
}

.progress-item {
  width: 48%;
}

.progress-title {
  font-size: 14px;
  color: white;
  margin-bottom: 10px;
  opacity: 0.9;
}

.timeline {
  height: 60px;
  background-color: rgba(255, 255, 255, 0.2);
  border-radius: 8px;
  position: relative;
  padding: 10px 5px;
}

.timeline-markers {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  height: 100%;
}

.marker {
  width: 12px;
  height: 12px;
  background-color: white;
  border-radius: 50%;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 10px;
  color: rgba(0, 0, 0, 0.6);
  font-weight: bold;
}

.timeline-line {
  position: absolute;
  bottom: 25px;
  left: 5px;
  right: 5px;
  height: 2px;
  background-color: rgba(255, 255, 255, 0.5);
  z-index: 1;
}

.progress-circle {
  width: 100%;
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

.circle-text {
  color: white;
  font-weight: bold;
  font-size: 18px;
}

.join-count {
  text-align: center;
  font-size: 14px;
  color: white;
  margin-top: 10px;
  opacity: 0.9;
}

/* 加载状态 */
.loading-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 200px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .project-grid {
    grid-template-columns: 1fr;
  }

  .progress-section {
    flex-direction: column;
  }

  .progress-item {
    width: 100%;
    margin-bottom: 15px;
  }
}
</style>
